package com.serkol.ams.controller.api;


import com.serkol.ams.bean.Result;
import com.serkol.ams.entity.SysCategory;
import com.serkol.ams.service.MiniPageService;
import com.serkol.ams.service.SysCategoryService;
import com.serkol.ams.service.UsersService;
import com.serkol.annotation.IsLogin;
import com.serkol.constant.AuthConstant;
import com.serkol.constant.ResultCode;
import com.serkol.constant.ResultConstant;
import com.serkol.exception.AuthException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/syscategory")
public class SysCategoryApiController {

    @Autowired
    private SysCategoryService categoryService;


    @Autowired
    private MiniPageService miniPageService;

    @Autowired
    private UsersService userService;
    @Value("${upload.host}")
    private String uploadhost;

    @GetMapping("/list")
    public Result list(@RequestParam(value = "type",defaultValue = "0") int type) {
        AuthConstant.setPathUrl(uploadhost);
        String language = AuthConstant.getLanguage();
        System.out.println("Param:"+type);
        Result result = new Result();
        if(type>0) {
            System.out.println("Param:1"+type);
            List<Map<String, Object>> list = categoryService.fetchSysCategoryList(type);

                result.setStatus(ResultConstant.SUCSESS);

                result.setData(list);

        }

        return result;
    }
    @IsLogin
    @PostMapping("/edit")
    public Result edit(@RequestBody SysCategory menu) {
        menu.setParentId(menu.getParentId() != null && menu.getParentId() != 0 ? menu.getParentId() : null);
        boolean flag = false;
        if (menu.getId() != null && menu.getId() > 0) {
            flag = categoryService.updateById(menu);
        } else {
            flag = categoryService.save(menu);
        }
        if (flag) {
            return Result.ok();
        } else {
            return Result.failure();
        }

    }
    @IsLogin
    @GetMapping("/listTree")
    public Result listTree() {
        AuthConstant.setPathUrl(uploadhost);
        Result result = new Result();
        List<Map<String, Object>> list = categoryService.fetchSysCategoryTreeList();
        if (!list.isEmpty()) {

            result.setStatus(ResultConstant.SUCSESS);

            result.setData(list);
        } else {
            result.setStatus(ResultConstant.EMPTIY);
        }

        return result;
    }
    @IsLogin
    @GetMapping("/listSelect")
    public Result listSelect() {
        Result result = new Result();
        List<Map<String, Object>> list = categoryService.listSelect();
        if (!list.isEmpty()) {

            result.setStatus(ResultConstant.SUCSESS);

            result.setData(list);
        } else {
            result.setStatus(ResultConstant.EMPTIY);
        }

        return result;
    }
    @IsLogin
    @GetMapping("/listPageSelect")
    public Result listPageSelect() {
        Result result = new Result();
        List<Map<String, Object>> list = miniPageService.listSelect();
        if (!list.isEmpty()) {

            result.setStatus(ResultConstant.SUCSESS);

            result.setData(list);
        } else {
            result.setStatus(ResultConstant.EMPTIY);
        }

        return result;
    }
    @IsLogin
    @PostMapping("/byDelete")
    @ResponseBody
    public Result byDelete(@RequestParam(value = "id", defaultValue = "0") long id) throws AuthException {
        if(id>0) {
            boolean b = categoryService.byDelete(id);
            if (b) {
                return Result.ok();
            } else {
                throw new AuthException(ResultCode.REQUEST_ERROR_NOT_ACCEPTABLE);
            }
        }else{
            throw new AuthException(ResultCode.REQUEST_ERROR_NOT_FOUND);
        }
    }
}
